package com.ruoyi.pay.utils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.util.Hashtable;

import com.baidu.aip.util.Base64Util;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.Base64Utils;
import sun.misc.BASE64Encoder;

/**
 * 二维码生成器
 *
 * @author 斌哥哥
 * @date 2019-12-31
 */
public class QrcodeGenerator {

    /**
     * 保存二维码图片
     *
     * @param text 二维码内容
     * @param width 宽度
     * @param height 高度
     * @param filePath 文件保存路径
     * @throws WriterException
     * @throws IOException
     */
    public static void generateQRCodeImage(String text, int width, int height, String filePath)
        throws WriterException, IOException {
        QRCodeWriter qrCodeWriter = new QRCodeWriter();
        BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height);
        Path path = FileSystems.getDefault().getPath(filePath);
        MatrixToImageWriter.writeToPath(bitMatrix, "PNG", path);
    }

    /**
     * 返回二维码字节数组
     * @param text
     * @param width
     * @param height
     * @return
     * @throws WriterException
     * @throws IOException
     */
    public static byte[] getQRCodeImage(String text, int width, int height) throws WriterException, IOException {
        QRCodeWriter qrCodeWriter = new QRCodeWriter();
        Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
        hints.put(EncodeHintType.MARGIN, 1);//去除多余白边
        BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height,hints);
        ByteArrayOutputStream pngOutputStream = new ByteArrayOutputStream();
        MatrixToImageWriter.writeToStream(bitMatrix, "PNG", pngOutputStream);
        byte[] pngData = pngOutputStream.toByteArray();
        //InputStream input = new ByteArrayInputStream(pngData);
        return pngData;
    }
    /**
     * 返回base64格式图片
     * @param text
     * @param width
     * @param height
     * @return
     * @throws WriterException
     * @throws IOException
     */
    public static String getBase64QRCodeImage(String text, int width, int height) throws WriterException, IOException {
        byte[] imgData = getQRCodeImage(text,width,height);
        BASE64Encoder encoder = new BASE64Encoder();
        String png_base64 = encoder.encode(imgData).trim();
        String imgStr = "data:image/jpg;base64," + png_base64.replaceAll("\n", "").replaceAll("\r", "");//删除 \r\n
        return imgStr;
    }
    public static void main(String[] args) {
        try {
            String a  = getBase64QRCodeImage("http://www.baidu.com", 350, 350);
            System.out.println(a.length());
        } catch (WriterException | IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
